package com.twotiger.and.adapter;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import app.twotiger.p2p.R;

import com.twotiger.and.bean.ProjectRepayPlan;
import com.twotiger.and.util.ArithUtils;
import com.twotiger.and.util.StringUtils;

public class DealPlanCalendarAdapter extends BaseAdapter {

    private ArrayList<ProjectRepayPlan> list;
    private LayoutInflater minflater;
    
    private final int TYPE_1 = 0; //有记录的布局类型
    private final int TYPE_2 = 1;  //无记录的布局类型

    public DealPlanCalendarAdapter(Activity activity, ArrayList<ProjectRepayPlan> list) {
        super();
        this.list = list;
        minflater = LayoutInflater.from(activity);
    }

    @Override
    public int getCount() {

        return list.size() > 0 ? list.size() : 1;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }
    
    @Override
    public int getItemViewType(int position) {
        if(list.size() > 0){
            return TYPE_1;  
        }else {
            return TYPE_2;   //无数据的布局
        }
    }
    
    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        
        MViewholder mViewHolder = null;
        MViewholderNoRecord mMViewholderNoRecord = null;
        final int _position = position;
        int type = getItemViewType(_position);
        
        //获得布局
        if (convertView == null) {
            switch (type) {
            case TYPE_1:
                mViewHolder = new MViewholder();
                convertView = minflater.inflate(R.layout.repayment_plan_calendar_item, null);
                mViewHolder.repayment_item_time = (TextView) convertView
                    .findViewById(R.id.repayment_item_time);
                mViewHolder.project_name = (TextView) convertView.findViewById(R.id.project_name);
                mViewHolder.money_text = (TextView) convertView.findViewById(R.id.money_text);
                mViewHolder.repayment_type = (TextView) convertView.findViewById(R.id.repayment_type);
                mViewHolder.iv_inverst_complete = (ImageView) convertView
                    .findViewById(R.id.iv_inverst_complete);
                convertView.setTag(mViewHolder);
                break;
            case TYPE_2:
                mMViewholderNoRecord = new MViewholderNoRecord();
                convertView = minflater.inflate(R.layout.repayment_plan_calendar_no_record_item, null);
                convertView.setTag(mMViewholderNoRecord);
                break;
            default:
                break;
            }
            
        } else {
            switch (type) {
            case TYPE_1:
                mViewHolder = (MViewholder) convertView.getTag();
                break;
            case TYPE_2:
                mMViewholderNoRecord = (MViewholderNoRecord) convertView.getTag();
                break;
            default:
                break;
            }
        }
        
        //设置数据
        switch (type) {
        case TYPE_1:
            ProjectRepayPlan inverst = list.get(_position);
            if ("1".equals(inverst.getCompleteMark())) { //已还款
                mViewHolder.iv_inverst_complete.setVisibility(View.VISIBLE);
            } else if ("0".equals(inverst.getCompleteMark())){ //未还款
                mViewHolder.iv_inverst_complete.setVisibility(View.GONE);
            }
            mViewHolder.money_text.setText(ArithUtils.coverMoneyComma((Double.parseDouble(inverst
                .getCapital()) + Double.parseDouble(inverst.getInterest())) + ""));
            if ("0.00".equals(inverst.getCapital())) {
                mViewHolder.repayment_type.setText("利息");
            } else if ("0.00".equals(inverst.getInterest())) {
                mViewHolder.repayment_type.setText("本金");
            } else {
                mViewHolder.repayment_type.setText("本金+利息");
            }
            mViewHolder.repayment_item_time.setText(StringUtils.formatString(inverst
                .getRepaymentTime().substring(0, 8)));
            if(StringUtils.isEmpty(inverst.getStatusName())){
                mViewHolder.project_name.setText(inverst.getProjectName());
            }else {
                mViewHolder.project_name.setText(inverst.getProjectName() + "["+ inverst.getStatusName()+"]");
            }
            break;
        case TYPE_2:
            
            break;
        default:
            break;
        }

        return convertView;
    }

    public void clearData() {
        list.clear();
        notifyDataSetChanged();
    }

    public void resetData(List<ProjectRepayPlan> repayments) {
        if (!repayments.isEmpty()) {
            list.clear();
            list.addAll(repayments);
            notifyDataSetChanged();
        }

    }

    public void addData(List<ProjectRepayPlan> repayments) {
        if (!repayments.isEmpty()) {
            list.addAll(repayments);
            notifyDataSetChanged();
        }
    }

    class MViewholder {
        public TextView repayment_type;
        public TextView money_text;
        public TextView project_name;
        public TextView repayment_item_time;
        public TextView message_time;
        public ImageView iv_inverst_complete;
    }
    
    class MViewholderNoRecord {
        
    }

}
